<template>
  <div style="padding: 15px;">
    <div style="margin-bottom: 15px;">
      <Input style="width: 200px;" placeholder="请输入教师姓名" v-model="searchName"/>
      <Select placeholder="请选择教师所属机构" style="width: 200px;" v-model="searchOrgName">
        <Option v-for="item in orgList" :value="item.id" :key="item.id">{{ item.name }}</Option>
      </Select>
      <Select style="width:200px" placeholder="请选择教师类型" v-model="searchTag">
        <Option value="1">名师</Option>
        <Option value="0">一般教师</Option>
      </Select>
      <Button @click="searchOrg" type="success">搜索</Button>
      <Button @click="add" type="primary" style="float: right;">添加教师</Button>
    </div>
    <Table border ref="selection" :columns="teacherPageTitle" :data="teacherPageList"></Table>
    <Page :total="total" show-sizer style="margin-top: 15px;" @on-change="pageChange" @on-page-size-change="pageSizeChange"/>
    <Modal v-model="orgTeacherModal" :title="modalTitle" @on-ok="submit" @on-cancel="cancel">
      <Form :model="orgTeacherForm" :label-width="80">
        <FormItem label="教师姓名">
          <Input v-model="orgTeacherForm.staffName" placeholder="请输入教师姓名"/>
        </FormItem>
        <FormItem label="所属机构">
          <Select v-model="orgTeacherForm.orgId">
            <Option v-for="item in orgList" :value="item.id" :key="item.id">{{ item.name }}</Option>
          </Select>
        </FormItem>
        <FormItem label="登录名">
          <Input v-model="orgTeacherForm.loginName" placeholder="请输入教师登录名"/>
        </FormItem>
        <FormItem label="联系电话">
          <Input v-model="orgTeacherForm.mobile" placeholder="请输入教师联系电话"/>
        </FormItem>
        <FormItem label="老师标签">
          <Select v-model="orgTeacherForm.sysLabel" multiple>
            <Option v-for="item in teacherTagList" :value="item.id" :key="item.id">{{ item.labelName }}</Option>
          </Select>
        </FormItem>
        <FormItem label="推荐名师">
          <Select v-model="orgTeacherForm.topTeacher">
            <Option value="1">是</Option>
            <Option value="0">否</Option>
          </Select>
        </FormItem>
        <FormItem label="状态">
          <i-switch size="large" v-model="orgTeacherForm.status">
            <span slot="open">启用</span>
            <span slot="close">禁用</span>
          </i-switch>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>

<script>
  import {formatDate} from '_com/date'
  export default {
    name: 'index',
    data () {
      return {
        searchName: '',
        searchOrgName: [],
        searchTag: '',
        number: 0,
        size: 10,
        total: 0,
        orgTeacherForm: {
          staffName: '',
          orgId: [],
          sysLabel: [],
          loginName: '',
          mobile: '',
          topTeacher: '',
          status: true
        },
        orgList: [],
        orgTeacherModal: false,
        modalTitle: '',
        teacherPageTitle: [
          {
            title: '教师姓名',
            key: 'staffName',
            align: 'center'
          },
          {
            title: '所属机构',
            key: 'orgName',
            align: 'center'
          },
          {
            title: '登录名',
            key: 'loginName',
            align: 'center'
          },
          {
            title: '联系电话',
            key: 'mobile',
            align: 'center'
          },
          {
            title: '是否为名师',
            align: 'center',
            render: (h, params) => {
              const row = params.row
              const color = row.topTeacher === '1' ? '#32e636' : row.topTeacher === '0' ? '#db2516' : ''
              const type = row.topTeacher === '1' ? 'md-checkmark-circle' : row.topTeacher === '0' ? 'md-close-circle' : ''
              return h('Icon', {
                props: {
                  type: type,
                  color: color,
                  size: 20
                }
              })
            }
          },
          {
            title: '创建时间',
            key: 'createTime',
            align: 'center',
            width: 150,
            render: (h, params) => {
              return h('div', formatDate(new Date(params.row.createTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '添加人',
            key: 'createName',
            align: 'center'
          },
          {
            title: '状态',
            key: 'status',
            width: 120,
            align: 'center',
            render: (h, params) => {
              const row = params.row
              const color = row.active === 1 ? 'primary' : row.active === 0 ? 'error' : ''
              const text = row.active === 1 ? '正常' : row.active === 0 ? '禁用' : ''
              return h('Tag', {
                props: {
                  type: 'dot',
                  color: color
                }
              }, text)
            }
          },
          {
            title: '操作',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      let row = params.row
                      this.editor()
                      this.orgTeacherForm.staffName = row.staffName
                      this.orgTeacherForm.orgId = row.orgId
                      this.orgTeacherForm.loginName = row.loginName
                      this.orgTeacherForm.mobile = row.mobile
                      this.orgTeacherForm.topTeacher = row.topTeacher
                      this.orgTeacherForm.id = row.id
                      this.orgTeacherForm.sysLabel = row.labelIds
                      if (row.active === 1) {
                        this.orgTeacherForm.status = true
                      } else {
                        this.orgTeacherForm.status = false
                      }
                    }
                  }
                }, '编辑'),
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '你确定要删除吗？',
                        okText: '确定',
                        cancelText: '取消',
                        onOk: () => {
                          this.remove(params.row.id)
                        }
                      })
                    }
                  }
                }, '删除')
              ])
            }
          }
        ],
        teacherPageList: [],
        teacherTagList: []
      }
    },
    mounted() {
      this.affiliationList()
      this.teacherPage()
      this.teacherTag()
    },
    methods: {
      teacherTag() {
        this.$get('/api/api/admin/label/list', {labelCategory: '1'}).then((res) => {
          if (res.code === 0) {
            this.teacherTagList = res.data
          }
        })
      },
      affiliationList() {
        let url = '/api/api/sys/org/list'
        this.$get(url).then((res) => {
          if (res.code === 0) {
            this.orgList = res.data
          }
        })
      },
      teacherPage() {
        let url = '/api/api/org/teacher/page'
        let form = {
          staffName: this.searchName,
          orgId: this.searchOrgName,
          topTeacher: this.searchTag,
          number: this.number,
          size: this.size
        }
        this.$get(url, form).then((res) => {
          if (res.code === 0) {
            this.teacherPageList = res.data
            this.total = res.total
          }
        })
      },
      title(title) {
        this.orgTeacherModal = true
        this.modalTitle = title
      },
      add() {
        this.title('添加教师')
      },
      editor() {
        this.title('编辑教师')
      },
      submit() {
        if (this.modalTitle === '添加教师') {
          this.addOrg()
        } else {
          this.theEditor()
        }
      },
      addOrg() {
        let url = '/api/api/org/teacher/add'
        let form = {
          staffName: this.orgTeacherForm.staffName,
          orgId: this.orgTeacherForm.orgId,
          loginName: this.orgTeacherForm.loginName,
          mobile: this.orgTeacherForm.mobile,
          topTeacher: this.orgTeacherForm.topTeacher,
          sysLabel: this.orgTeacherForm.sysLabel,
          status: Number(this.orgTeacherForm.status)
        }
        this.$post(url, form).then((res) => {
          if (res.code === 0) {
            this.teacherPage()
            this.cancel()
            this.$Modal.info({
              title: '添加成功',
              content: '默认密码为：123456'
            })
          } else {
            this.$Message.error('添加失败')
          }
        })
      },
      theEditor() {
        let url = '/api/api/org/teacher/edit'
        let form = {
          staffName: this.orgTeacherForm.staffName,
          orgId: this.orgTeacherForm.orgId,
          loginName: this.orgTeacherForm.loginName,
          mobile: this.orgTeacherForm.mobile,
          topTeacher: this.orgTeacherForm.topTeacher,
          sysLabel: this.orgTeacherForm.sysLabel,
          status: Number(this.orgTeacherForm.status),
          id: this.orgTeacherForm.id
        }
        this.$put(url, form).then((res) => {
          if (res.code === 0) {
            this.$Message.success('编辑成功')
            this.teacherPage()
            this.cancel()
          } else {
            this.$Message.error('编辑失败')
          }
        })
      },
      remove(id) {
        this.$deletes('/api/api/org/teacher/delete', {id: id}).then((res) => {
          if (res.code === 0) {
            this.$Message.success('删除成功')
            this.teacherPage()
          } else {
            this.$Message.error('删除失败')
          }
        }).catch((res) => {
          this.$Message.error('删除失败')
        })
      },
      searchOrg() {
        this.teacherPage()
      },
      cancel() {
        this.orgTeacherForm = {}
        this.orgTeacherForm.status = true
      },
      pageChange(page) {
        this.number = page - 1
        this.teacherPage()
      },
      pageSizeChange(page) {
        this.size = page
        this.teacherPage()
      }
    }
  }
</script>

<style scoped>

</style>
